package com.gitlab.lucky.common.service;

import com.alibaba.fastjson.JSONObject;
import com.gitlab.lucky.common.dao.mapper.ConfigDataMapper;
import com.gitlab.lucky.common.dao.model.ConfigData;
import com.gitlab.lucky.common.enums.Constant;
import com.gitlab.lucky.common.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @program: lucky
 * @author: Mr.Han
 * @create: 2018-11-20 10:12
 **/
@Service
public class ConfigDataService extends ServiceImpl<ConfigDataMapper,ConfigData> {
    @Autowired
    private RedisService redisService;
    //取全局配置信息
    public JSONObject getConfig(Integer platform){
        //通过平台取配置信息
        JSONObject config = (JSONObject)redisService.get(Constant.CONFIG_KEY+platform);
        if(config==null){
            JSONObject jsonObject = new JSONObject();
            Example example = new Example(ConfigData.class);
            example.createCriteria().andEqualTo("platform",platform);
            List<ConfigData> configDataList = this.selectByExample(example);
            for (ConfigData configData : configDataList) {
                jsonObject.put(configData.getConfigKey(),configData.getConfigValue());
            }
            redisService.set(Constant.CONFIG_KEY+platform,jsonObject);
            return jsonObject;
        }
        return config;
    }
}
